package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.PurchaseDetailAdapter
import com.hinnotek.amoeba.bean.old.CompanyContractByIdBean
import com.hinnotek.amoeba.bean.old.ContractBean
import com.hinnotek.amoeba.databinding.ActivityContractDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.ContractViewModel
import com.lee.common.network.net.ObserverState

class ContractDetailActivity : BaseActivity<ActivityContractDetailBinding>() {

    private lateinit var mAdapter: PurchaseDetailAdapter
    private val testBeanList: MutableList<ContractBean> = mutableListOf()
    private var customDialog: CustomDialog? = null

    val viewModel by viewModels<ContractViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("合同签署")

        setContentView(R.layout.activity_contract_detail)
        initVew()
    }

    override fun initVew() {
        val manager = LinearLayoutManager(this)
        manager.orientation = RecyclerView.VERTICAL
        bindingView.rlPurchase.setLayoutManager(manager)
        bindingView.rlPurchase.adapter = PurchaseDetailAdapter(testBeanList).also { mAdapter = it }
        customDialog = CustomDialog(mContext, "正在加载...")
        customDialog?.show()
        setData()
    }



    override fun setData() {
        val companyContractId = intent.getIntExtra("companyContractId", 0)
        getCompanyContractById(companyContractId)
    }

    /**
     * app端获取合同列表分页接口
     */
    fun getCompanyContractById(companyContractId: Int) {
        viewModel.getCompanyContractByIdData(companyContractId)
            .observe(this, object : ObserverState<CompanyContractByIdBean>() {
                override fun onDataChange(data: CompanyContractByIdBean) {
                    customDialog?.dismiss()
                    testBeanList.add(ContractBean(0, data, null))
                    for (i in data.companyContractProjects.indices) {
                        testBeanList.add(
                            ContractBean(
                                1,
                                null,
                                data.companyContractProjects[i]
                            )
                        )
                    }
                    mAdapter.notifyDataSetChanged()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog?.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }

            })

    }

}